package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.exception.LogicException;
import cn.wolfcode.wolf2w.interceptor.RequireLogin;
import cn.wolfcode.wolf2w.redis.IStrategyRedisService;
import cn.wolfcode.wolf2w.redis.IUserInfoRedisService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("users")
public class UserInfoController {

    /*获取配置文件中短信的接口请求地址与凭证,springboot启动时会自动解析去配置文件中相对应的值*/
    @Value("${sms.url}")
    private String smsurl;
    @Value("${sms.appkey}")
    private String appkey;


    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private IStrategyRedisService strategyRedisService;

    @Autowired
    private IUserInfoRedisService userInfoRedisService;

    @GetMapping("checkPhone")
    public Object checkPhone(String phone) {
        boolean ret = userInfoService.findByPhone(phone);
        return ret;
    }

    @GetMapping("sendVerifyCode")
    public Object sendVerifyCode(String phone) {
        userInfoService.sendVerifyCode(phone, smsurl, appkey);
        return JsonResult.success();
    }

    /*
     * 注册用户信息
     * */
    @PostMapping("regist")
    public Object regist(String phone, String nickname, String password, String rpassword, String verifyCode) {
        userInfoService.regist(phone, nickname, password, rpassword, verifyCode);
        return JsonResult.success();
    }


    /*
     * 登录验证
     * */
    @PostMapping("login")
    public Object login(String username, String password) {
        UserInfo user = userInfoService.login(username, password);
        //将以token为key，userInfo为value缓存到redis中，供验证使用
        String token = userInfoRedisService.setToken(user);

        //将userInfo对象,token作为JsonResult返回到object类型的data属性返回到浏览器中
        //又由于起返回的格式为json格式,需要使用kv对这种格式,可以使用map将userInfo对象,token装好返回
        HashMap<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("user", user);
        return JsonResult.success(map);
    }

    @RequireLogin
    @GetMapping("currentUser")
    public Object currentUser(HttpServletRequest request) {
        //获取浏览器请求头中带过来的token
        String token = request.getHeader("token");
        //通过token作为key查询redis中的value(用户对象)
        UserInfo user = userInfoRedisService.getUserInfo(token);
        return JsonResult.success(user);
    }

    @GetMapping("strategies/favor")
    public Object favorView(String sid,String userId){
         List<String> sids =  strategyRedisService.favorView(sid,userId);
         return JsonResult.success(sids);
    }

    @GetMapping("get")
    public Object get(String id){
        UserInfo userInfo = userInfoService.get(id);
        return JsonResult.success(userInfo);
    }
}
